import time

from openpyxl.workbook import Workbook
from selenium import webdriver
from selenium.common import NoSuchWindowException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

wb = Workbook()
ws = wb.active
headers = ['公司名称', '电话', '手机', '传真', '地址', '联系人']
ws.append(headers)
name = []
telephone = []
Cell_phone = []
fax = []
address = []
people = []
resp1 = []


class Albb:
    # 定义测试方法-账号不存在
    def login(self):
        url_list = ['yichuang2017', 'shop4z14079262175', 'shop0z7671u478921', 'shop8088306j77318', 'shop90608087880z7',
                    'gangdi', 'shop718i757657038', 'shop8d30l0v838006', 'weiyoujs', 'shop71m9739183980',
                    'shop70399653r5411', 'shengmaicnc', 'shop6085496150497', 'shop41790112295e5', 'shop06j1003g39202',
                    'shop1470070306803', 'shop838691v735355', 'shop540ew34248583', 'cfsshilihe', 'shop0662844yxs936',
                    'shop1j558i09306i1', 'shop8z0101142j2x7', 'shop6tc2469152709', 'hxb868', 'mairte',
                    'shop12r5978641729', 'shop1475945616116', 'shop9741o0g459g58', 'shengda98', 'dgqianyou',
                    'baidefuwujin', 'nbkwwj', 'shop630b186l28764', 'shenzhenjiecheng', 'shop106951t6tf261', 'gzlcbj',
                    'shop255140a08y3o7', 'lishengjingmi', 'shop9c47b40512847', 'shop0w629114302d0', 'shop4151759710474',
                    'shop4v077h3v10665', 'shop1433782759217', 'shop31p202p5i3046', 'shop06y724p505970', 'yideli2010',
                    'szhongtailang', 'shop1474648815616', 'shop237y0555u2559', 'hongjiejmwj', 'huixinyuan2007',
                    'shop3521837o93044', 'shop6872706pb79i2', 'shop03qk3970s6705', 'wxachi', 'shop8594b6kq67002',
                    'shop698n5139214k5', 'dgsgoodwj', 'shop6u24r371480v4', 'shhehuajx', 'chuntaisuye',
                    'shop5234b84889148', 'shop1380214730897', 'shop935602y13b8f5', 'xmmxjx', 'wanfuxin',
                    'shop6753380988424', 'shop6632w4287t745', 'hlcncjg', 'shop69uu613l81882', 'dgzrwjzp',
                    'shop1359r23202533', 'gearept', 'xhsaybxg', 'shop631si04i17865', 'lmachining', 'shop93683i4607223',
                    'shop4398712192142', 'zurongwujin', 'shop687v0a1216914', 'shop606850264ylf0', 'shop123b42865pr74',
                    'shop43646446591t5', 'dgztjmwj', 'heshenjixiejg', 'chenlbxg', 'shop61a7vk608t7t7',
                    'shop44e496948i046', 'guanyi5188', 'shop6m9om41994667', 'tuoyuewujin', 'shop521p368k168m3',
                    'dgjnsy', 'dgqianduoduo', 'shop26618388g7330', 'shop71x550950m191', 'shop8y1503y3z4717',
                    'yhxzhizao', 'hwrobots', 'shop45u7c51660858', 'shop5n6n6i9510463', 'shop967o464k97426',
                    'shop6539370jut750', 'shop34q5270144cs2', 'ysmetal', 'shop40676va9221h9', 'shop92m820k821793',
                    'shop538m120515m50', 'shop9d6618130q809', 'shop23460w76697x0', 'shenzhenxuzhen',
                    'shop8gf5654931809', 'kaihengsujiao', 'szfuwanglong', 'shop3886j6f503449', 'shop982f056475r87',
                    'shop3p264922924z5', 'shop38468eq107601', 'shop06w4970m27130', 'dxjmjx', 'myzxdeng',
                    'shop1413391507087', 'huawangjinshu', 'shop77g0416527916', 'xiangchengtech', 'zhonglingjx',
                    'szxyh888888', 'dglongwang', 'shop1429289554859', 'shop73969681y9nu6', 'shop7d768c2577523',
                    'gddzwj', 'shop0b6381264q731', 'shop775k8q7725432', 'abprecisionparts', 'shop9419231y51k90',
                    'shop7v1446389v671', 'shop223bb66q73342', 'shch13816428283', 'zyjmwj', 'shop91o2w455342q1',
                    'binxiangjingmi', 'shop000525o31t353', 'shop760s9j5579218', None, 'shop14563330am0m6',
                    'shop62040a6c2c955', 'shop2121c1227bi75', 'shop03236191ik514', 'chuanyunjixie', 'boyuelong',
                    'shop28872r6r1n315', 'shop09j77p78j9534', 'shop435l2sh961967', 'shop75730ti7610y5',
                    'shop54dj28k364305', 'shop526119015mgm4', 'mingyixin', 'shop4n462229u4438', 'sanshengguangxue',
                    'shop9759f2n758055', 'shop17o0xo6366782', 'hongshengjingmi', 'shop860a136887124', 'dgyiweiwujin',
                    'shop6v617y7324014', 'shop1488935141185', 'heiang', 'shop0001465853t28', 'gangqiwujin',
                    'xinyunyang', 'shop709r4p7627j87', 'shop12s9452b71h66', 'shop36986r2439407', 'shop1489510749728',
                    'shop3284s4x686f17', 'shop98348813bf313', 'kwanlonmed', 'xhlfbxg', 'jinduobuxiugang',
                    'shop7105l93c7u209', 'shop38138h5465490', 'aozhihong', 'szhuayi8899', 'shop60l62867686k8',
                    'shzc5188', 'shop15979b9b68w78', 'shop1428080761527', 'shop4462r0172z562', 'shop99r8592535008']
        driver = webdriver.Chrome()  # 创建浏览器驱动对象
        driver.maximize_window()  # 窗口最大化
        options = webdriver.ChromeOptions()
        # 此步骤很重要，设置为开发者模式，防止被各大网站识别出来使用了Selenium
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        options.add_argument('--disable-blink-features')
        options.add_argument('--disable-blink-features=AutomationControlled')  # 去除浏览器selenium监控
        # options.add_argument('--headless')  # 浏览器不提供可视化页面
        options.add_argument('--disable-gpu')  # 禁用GPU加速
        driver = webdriver.Chrome(options=options)
        driver.get("https://login.1688.com/member/signin.htm")  # 打开首页面
        driver.implicitly_wait(10)  # 隐式等待
        # 扫码登录
        driver.find_element(By.XPATH, '//*[@id="login"]/div[1]/i').click()
        time.sleep(20)
        # 点击服务按钮
        driver.find_element(By.XPATH, '//*[@id="topbar-box"]/div[2]/div/div[1]/div/div[2]').click()
        time.sleep(2)
        # 点击产品按钮
        driver.find_element(By.XPATH, '//*[@id="topbar-box"]/div[2]/div/div[1]/div/div[3]/ul/li[2]/span').click()
        time.sleep(2)
        # 商品输入框
        driver.find_element(By.XPATH, '//*[@id="topbar-box"]/div[2]/div/div[2]/input').send_keys('精密加工件 cnc')
        time.sleep(2)
        # 搜索按钮
        driver.find_element(By.XPATH, '//*[@id="topbar-box"]/div[2]/div/button').click()
        time.sleep(2)
        # 刷新页面
        driver.refresh()
        time.sleep(2)
        for x in url_list:
            try:
                # 执行JavaScript打开新的窗口
                driver.execute_script("window.open();")
                all_handles = driver.window_handles
                new_window_handle = [handle for handle in all_handles if handle != driver.current_window_handle][0]
                driver.switch_to.window(new_window_handle)
                #     #
                #     # # 获取所有窗口句柄
                #     # all_handles = driver.window_handles
                #
                #     # # 切换到新的窗口
                #     # new_window = [handle for handle in all_handles if handle != driver.current_window_handle][0]
                #     # driver.switch_to.window(new_window)
                #
                # 在新窗口中打开一个新的网址
                driver.get('https://{}.1688.com/page/contactinfo.htm?spm=0.0.wp_pc_common_topnav_38229151.0'.format(x))
                driver.refresh()
                time.sleep(2)
                # 检查是否出现了滑动验证码
                try:
                    slider = driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
                    # ActionChains(driver).click_and_hold(slider).perform()  # 按住滑块
                    actions = ActionChains(driver)
                    actions.move_to_element(slider).click_and_hold()
                    for i in range(50, 350, 50):
                        actions.move_by_offset(i, 0)
                        actions.pause(0.5)
                    actions.release()
                    actions.perform()
                    time.sleep(1)  # 按住滑块释放时间
                    driver.refresh()
                    time.sleep(2)
                except:
                    pass
                # 检查是否出现了滑动验证码
                try:
                    slider = driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
                    # ActionChains(driver).click_and_hold(slider).perform()  # 按住滑块
                    actions = ActionChains(driver)
                    actions.move_to_element(slider).click_and_hold()
                    for i in range(60, 350, 60):
                        actions.move_by_offset(i, 0)
                        actions.pause(0.5)
                    actions.release()
                    actions.perform()
                    time.sleep(2)
                    driver.refresh()
                    time.sleep(2)
                except:
                    pass
                # 检查是否出现了滑动验证码
                try:
                    slider = driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
                    # ActionChains(driver).click_and_hold(slider).perform()  # 按住滑块
                    actions = ActionChains(driver)
                    actions.move_to_element(slider).click_and_hold()
                    temp = [50, 100, -50, 200, 300]
                    for i in temp:
                        actions.move_by_offset(i, 0)
                        actions.pause(1)
                    actions.release()
                    actions.perform()
                    time.sleep(1)  # 按住滑块释放时间
                    driver.refresh()
                    time.sleep(2)
                except:
                    pass
                try:
                    slider = driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
                    # ActionChains(driver).click_and_hold(slider).perform()  # 按住滑块
                    actions = ActionChains(driver)
                    actions.move_to_element(slider).click_and_hold()
                    for i in range(50, 400, 50):
                        actions.move_by_offset(i, 0)
                        actions.pause(0.5)
                    actions.release()
                    actions.perform()
                    time.sleep(4)
                    driver.refresh()
                    time.sleep(2)
                except:
                    pass
                    # 检查是否出现了滑动验证码
                try:
                    slider = driver.find_element(By.XPATH, '//*[@id="nc_1_n1z"]')
                    # ActionChains(driver).click_and_hold(slider).perform()  # 按住滑块
                    actions = ActionChains(driver)
                    actions.move_to_element(slider).click_and_hold()
                    for i in range(60, 350, 60):
                        actions.move_by_offset(i, 0)
                        actions.pause(0.5)
                    actions.release()
                    actions.perform()
                    time.sleep(5)
                    driver.refresh()
                    time.sleep(2)
                except:
                    pass
                    # 检查是否出现了滑动验证码
                try:
                    merchant_name_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[2]'
                    merchant_name = driver.find_element(By.XPATH, merchant_name_path).text
                    # print(merchant_name)
                    name.append(merchant_name)
                    merchant_telephone_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[1]/div[2]'
                    merchant_telephone = driver.find_element(By.XPATH, merchant_telephone_path).text
                    # print(merchant_telephone)
                    telephone.append(merchant_telephone)
                    merchant_Cell_phone_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[2]/div[2]'
                    merchant_Cell_phone = driver.find_element(By.XPATH, merchant_Cell_phone_path).text
                    # print(merchant_Cell_phone)
                    Cell_phone.append(merchant_Cell_phone)
                    merchant_fax_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[3]/div[2]'
                    merchant_fax = driver.find_element(By.XPATH, merchant_fax_path).text
                    # print(merchant_fax)
                    fax.append(merchant_fax)
                    merchant_address_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[4]/div[2]'
                    merchant_address = driver.find_element(By.XPATH, merchant_address_path).text
                    # print(merchant_address)
                    address.append(merchant_address)
                    merchant_people_path = '//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[4]/div[3]'
                    merchant_people = driver.find_element(By.XPATH, merchant_people_path).text
                    # print(merchant_people)
                    people.append(merchant_people)
                    resp1.append(x)
                    print('{}已经爬取'.format(x))
                    info = [[merchant_name, merchant_telephone, merchant_Cell_phone, merchant_fax, merchant_address,
                             merchant_people]]
                    for i in info:
                        ws.append(i)
                    wb.save(r"G:\1688\info.xlsx")
                    time.sleep(2)
                    driver.refresh()
                    driver.close()
                    driver.switch_to.window(driver.window_handles[0])
                    time.sleep(1)
                except Exception as e:
                    print('该ID:{}没有获取到'.format(x), e)
                    pass


            except NoSuchWindowException:
                print("新窗口未成功打开或已被关闭，跳过当前URL。")
        driver.quit()

        # 现在你可以在新窗口中执行其他的操作了


resp = Albb()
resp.login()
print(name)
print(telephone)
print(Cell_phone)
print(fax)
print(address)
print(people)
